package dhadi.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import dhadi.model.Move;
import dhadi.model.SerializationContext;

public class MoveReader {
    
    BufferedReader reader = null;
    List<String> moveStrings;
    public MoveReader(File file) throws IOException {
        try {
            reader = 
                new BufferedReader(new InputStreamReader(
                    new FileInputStream(file)));
            moveStrings = new ArrayList<String>(50);
            String line;
            while ( (line = reader.readLine()) != null ) {
                line = line.trim();
                if (line.length() == 0) continue;
                moveStrings.add(line);
            }
        } finally {
            if (reader != null) reader.close();
        }
    }
    
    public List<Move> getMoves(SerializationContext context) throws JSONException {
         List<Move> moves = new ArrayList<Move>();
        for (String moveString : moveStrings) {
            moves.add(Move.fromJson(moveString,context));
        }
        return moves;
    }
}
